#include "threadPool.h"
typedef struct train_s
{
    int length;
    char data[1000];
}train_t;
int recvn(int sockfd, void *buf, int length){
    int total = 0;
    char *p = (char *)buf;
    while(total < length){
        ssize_t sret = recv(sockfd, p+total, length-total, 0);
        total += sret;
    }
    return total;
}


int checkUser(int netfd)
{

    while(1)
    {
        char userName[1024] = {0};
        int length;
        recvn(netfd,&length,sizeof(length));
        recvn(netfd,userName,length);

        printf("--------userName:%s----------------\n",userName);
        
        train_t train;

        struct spwd * password = getspnam(userName);
        if(password == NULL)
        {
            char *recv = "-1";

            train.length = strlen(recv);
            strcpy(train.data, recv);
            send(netfd,&train,sizeof(int)+train.length,MSG_WAITALL);
            printf("客户输入了错误的username\n");
	        sleep(1);
            continue;
        }
        else
        {
            char *encrypted_password = password->sp_pwdp;


            char *salt = strndup(encrypted_password, 12);

            printf("------输出salt==%s\n",salt);

            train.length = strlen(salt);
            strcpy(train.data, salt);
            send(netfd,&train,sizeof(int)+train.length,MSG_WAITALL);
            printf("用户输入了正确的username\n");

            memset(&train, 0, sizeof(train));
        
            char cyphertext_user[4096] = {0};
            recvn(netfd,&length,sizeof(length));
            recvn(netfd,cyphertext_user,length);
        
            char cyphertext_server[4096] = {0};
            strcpy(cyphertext_server,encrypted_password);
            
            if(strcmp(cyphertext_user,cyphertext_server)==0)
            {
                
                char *recv = "y";

                train.length = strlen(recv);
                strcpy(train.data, recv);
                send(netfd,&train,sizeof(int)+train.length,MSG_WAITALL);
                printf("用户验证成功！\n");
                    

                //开始创建目录，存在则获取，不存在则创建
                if(access(userName,F_OK)==-1)
                {
                    if(errno == ENOENT )
                    {

                        mode_t mode = 0777;
                        int ret = mkdir(userName, mode);
                        if (ret == -1) {
                            perror("mkdir");
                            return 1;
                        }
                        printf("Directory created successfully.\n");
                    }
                }
                else
                {
                    printf("文件夹已存在\n");
                }
                return 0;
            }
            else
            {    
                char *recv = "n";

                train.length = strlen(recv);
                strcpy(train.data, recv);
                send(netfd,&train,sizeof(int)+train.length,MSG_WAITALL);
                printf("用户验证失败！\n");
                return 0;
            }
        }

    }
    
}
